package client;

import java.awt.Component;
import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class DBDialog extends JDialog implements ActionListener{
	private JLabel dbURLLabel = new JLabel("Database URL:");
	private JLabel dbLoginLabel = new JLabel("Login:");
	private JLabel dbPasswordLabel = new JLabel("Password:");
	private JLabel choiceLabel = new JLabel("Remember my choice");
	
	private JTextField dbURLField = new JTextField();
	private JTextField dbLoginField= new JTextField();
	private JTextField dbPasswordField= new JTextField();
	
	private JCheckBox checkBox = new JCheckBox();
	
	private JButton okButton = new JButton("OK");
	private JButton cancelButton = new JButton("Cancel");
	
	private String dbURL = "";
	private String dbLogin = "";
	private String dbPassword = "";
	
	ActionListener listener;
	
	Properties properties;
	
	public String getDbURL() {
		return dbURL;
	}
	public void setDbURL(String dbURL) {
		this.dbURL = dbURL;
	}
	public String getDbLogin() {
		return dbLogin;
	}
	public void setDbLogin(String dbLogin) {
		this.dbLogin = dbLogin;
	}
	public String getDbPassword() {
		return dbPassword;
	}
	public void setDbPassword(String dbPassword) {
		this.dbPassword = dbPassword;
	}
	
	public DBDialog(Properties properties){
		this.properties = properties;
/*		properties = new Properties("dbconf.xml");*/
		this.dbURL = this.properties.getProperty("db_url");
		this.dbLogin = this.properties.getProperty("login");
		this.dbPassword = this.properties.getProperty("password");
		
		Container cp = this.getContentPane();

		this.setSize(400, 300);
		this.setResizable(false);
		
		setTitle("Select gatabase");
		setDefaultCloseOperation (JFrame.DO_NOTHING_ON_CLOSE);	
		
		GridBagLayout gbl = new GridBagLayout();
		GridBagConstraints gbc = new GridBagConstraints();
		
		cp.setLayout(gbl);
		
		gbc.fill = GridBagConstraints.BOTH;
		gbc.gridx = 0;
		gbc.gridy = 0;
		gbc.weightx = GridBagConstraints.REMAINDER;
		this.addComponent(cp, dbURLLabel, gbl, gbc);
		
		gbc.gridx = 0;
		gbc.gridy = 1;
		gbc.gridwidth = 2;
		gbc.insets = new Insets(5, 5, 5, 5);
		this.addComponent(cp, dbURLField, gbl, gbc);
		
		gbc.gridx = 0;
		gbc.gridy = 2;
		gbc.gridwidth = 1;
		gbc.insets = new Insets(0, 0, 0, 0);
		this.addComponent(cp, dbLoginLabel, gbl, gbc);
		
		gbc.gridx = 0;
		gbc.gridy = 3;
		gbc.gridwidth = 2;
		gbc.insets = new Insets(5, 5, 5, 5);
		this.addComponent(cp, dbLoginField, gbl, gbc);
		
		gbc.gridx = 0;
		gbc.gridy = 4;
		gbc.gridwidth = 1;
		gbc.insets = new Insets(0, 0, 0, 0);
		this.addComponent(cp, dbPasswordLabel, gbl, gbc);
		
		gbc.gridx = 0;
		gbc.gridy = 5;
		gbc.gridwidth = 2;
		gbc.insets = new Insets(5, 5, 5, 5);
		this.addComponent(cp, dbPasswordField, gbl, gbc);
		
		gbc.gridx = 0;
		gbc.gridy = 6;
		gbc.gridwidth = 1;
		gbc.weightx = GridBagConstraints.RELATIVE;
		gbc.insets = new Insets(0, 0, 0, 0);
		this.addComponent(cp, choiceLabel, gbl, gbc);
		
		gbc.gridx = 1;
		gbc.gridy = 6;
		gbc.weightx = GridBagConstraints.REMAINDER;
		gbc.fill = GridBagConstraints.NONE;
		gbc.anchor = GridBagConstraints.EAST;
		gbc.gridwidth = 1;
		gbc.insets = new Insets(5, 5, 5, 5);
		this.addComponent(cp, checkBox, gbl, gbc);
		
		gbc.anchor = GridBagConstraints.CENTER;
		gbc.gridx = 0;
		gbc.gridy = 7;
		gbc.weightx = 0.5;
		gbc.insets = new Insets(15, 5, 15, 5);
		this.addComponent(cp, okButton, gbl, gbc);
		
		gbc.gridx = 1;
		gbc.gridy = 7;
		gbc.weightx = 0.5;
		this.addComponent(cp, cancelButton, gbl, gbc);
		
		okButton.setActionCommand("selectdb");
		cancelButton.setActionCommand("dbselection_cancelled");
		okButton.addActionListener(this);
		cancelButton.addActionListener(this);
		
		dbURLField.setText(dbURL);
		dbLoginField.setText(dbLogin);
		dbPasswordField.setText(dbPassword);
		
		this.setModal(true);
	}
	protected void addComponent (Container cp, Component comp, GridBagLayout gridbag, GridBagConstraints gbc) {
		gridbag.setConstraints(comp, gbc);
		cp.add(comp);
	}
	public void actionPerformed(ActionEvent e){
		if(e.getActionCommand() == "selectdb"){
			properties.setProperty("db_url", dbURLField.getText());
			properties.setProperty("login", dbLoginField.getText());
			properties.setProperty("password", dbPasswordField.getText());
			if(checkBox.isSelected())
				properties.writePropertiesInFile("configs/client/db_conf.xml");
		}
		this.setVisible(false);
	}
	public Properties getProperties(){
		return properties;
	}
}
